package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.service.PortRPCService;
import id.net.iconpln.bb.client.service.PortRPCServiceAsync;
import id.net.iconpln.bb.client.tools.ComboPembangkit;

import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbBaBongkar extends LayoutContainer{
	private ComboBox cboPembangkit;
	private TextField txtNoBongkar;
	private final PortRPCServiceAsync service = GWT.create(PortRPCService.class);
	
	@Override
	protected void onRender(Element parent, int index) {
		// TODO Auto-generated method stub
		super.onRender(parent, index);
		
		setLayout(new FitLayout());	
		
		ContentPanel cp = new ContentPanel();  
	    cp.setHeaderVisible(false);
	    cp.setFrame(true);  
	    cp.setLayout(new FormLayout()); 
	    
	    final FormPanel panel = new FormPanel();	    
	    panel.setHeaderVisible(false);
	    
	    FormLayout layout = new FormLayout();  
	    layout.setLabelWidth(120);  
	    FieldSet fieldSet = new FieldSet();  
	    fieldSet.setHeading("Data Rakor");  
	    fieldSet.setCollapsible(false);
	    fieldSet.setLayout(layout); 
	    
	    cboPembangkit = new ComboPembangkit();
	    txtNoBongkar = new TextField();
	    txtNoBongkar.setFieldLabel("No Bongkar");
	    
	    fieldSet.add(cboPembangkit);
	    fieldSet.add(txtNoBongkar);
	    panel.add(fieldSet);
	    
	    panel.addButton(new Button("Cetak", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {  
		    	  Map map = new FastMap(); 
		    	  map.put("no_bongkar", txtNoBongkar.getRawValue());
		    	  map.put("path", "report/Berita_acara_pembokaran1.jasper");
		    	  
		    	  service.getLaporanBaBongkar("ba_bongkar", map, new AsyncCallback<List<Map>>() {
					
					@Override
					public void onSuccess(List<Map> result) {
						// TODO Auto-generated method stub
						if (result.get(0).get("message").toString().equals("1")){							
							Window win = new  Window();
							win.setUrl(GWT.getHostPageBaseURL() + "report.pdf");
							win.show();
						}
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						GWT.log(caught.getMessage());
					}
				});
		      }  
	    }));  
	    
	    panel.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {  
		        panel.reset();
		      }  
	    }));  
	    
	    cp.add(panel);
	    
	    add(cp);
	}
}
